package edu.ntu.im.p2ptrade.msg.handle;

import net.jxta.endpoint.Message;
import edu.ntu.im.p2ptrade.P2PTrade;
import edu.ntu.im.p2ptrade.msg.AbortCommit;
import edu.ntu.im.p2ptrade.msg.ApproveTRequest;
import edu.ntu.im.p2ptrade.msg.CanCommit;
import edu.ntu.im.p2ptrade.msg.CannotCommit;
import edu.ntu.im.p2ptrade.msg.Commit;
import edu.ntu.im.p2ptrade.msg.DenyTRequest;
import edu.ntu.im.p2ptrade.msg.GenericDialog;
import edu.ntu.im.p2ptrade.msg.GrantLock;
import edu.ntu.im.p2ptrade.msg.HeartBeat;
import edu.ntu.im.p2ptrade.msg.MyItems;
import edu.ntu.im.p2ptrade.msg.OKCommit;
import edu.ntu.im.p2ptrade.msg.PMessage;
import edu.ntu.im.p2ptrade.msg.QueryItem;
import edu.ntu.im.p2ptrade.msg.QueryUser;
import edu.ntu.im.p2ptrade.msg.SearchItemQuery;
import edu.ntu.im.p2ptrade.msg.SearchResult;
import edu.ntu.im.p2ptrade.msg.TRequest;
import edu.ntu.im.p2ptrade.msg.UserInfo;

public class PMessageHandleFactory {
	private PMessageHandle okcommit = new OKCommitHandle();

	private PMessageHandle genericDialog = new GenericDialogHandle();

	private PMessageHandle cancommit = new CanCommitHandle();

//	private PMessageHandle denyLock = new DenyLockHandle();

	private PMessageHandle trequest = new TRequestHandle();

	private PMessageHandle appReq = new ApproveTRequestHandle();

	private PMessageHandle denyReq = new DenyTRequestHandle();

//	private PMessageHandle unlock = new UnlockHandle();

	private PMessageHandle queryItem = new QueryItemHandle();

	private PMessageHandle myItem = new MyItemHandle();

	private PMessageHandle queryUser = new QueryUserHandle();

	private PMessageHandle userInfo = new UserInfoHandle();

	private PMessageHandle heartbeathandle = new HeartBeatHandle();

	private PMessageHandle searchItemQueryHandle = new SearchItemQueryHandle();

	private PMessageHandle searchResultHandle = new SearchResultHandle();

	private PMessageHandle abortTRequestHandle = new AbortCommitHandler();
	
	private PMessageHandle cannotCommitHandle = new CannotCommitHandle();
	
	private PMessageHandle commitHandle = new CommitHandle();
	private static PMessageHandleFactory me;

	public static PMessageHandleFactory getInstance() {
		if (me == null) {
			me = new PMessageHandleFactory();
		}
		return me;
	}

	private PMessageHandleFactory() {

	}

	public PMessageHandle getHandle(Message msg) {
		String type = msg.getMessageElement(
				P2PTrade.getInstance().getAppName(), PMessage.TYPE).toString();
		if (!type.equals(HeartBeat.class.getSimpleName())) {
			System.out.println("Type:" + type);
		}
		if (type.equals(GenericDialog.class.getSimpleName())) {
			return new AuthenticatedMessageHandlerWrapper(genericDialog);
		}  else if (type.equals(CanCommit.class.getSimpleName())) {
			return cancommit;
		} else if (type.equals(OKCommit.class.getSimpleName())) {
			return okcommit;
		} else if (type.equals(TRequest.class.getSimpleName())) {
			return new AuthenticatedMessageHandlerWrapper(trequest);
		} else if (type.equals(ApproveTRequest.class.getSimpleName())) {
			return new AuthenticatedMessageHandlerWrapper(appReq);
		} else if (type.equals(CannotCommit.class.getSimpleName())) {
			return cannotCommitHandle;
		} else if (type.equals(DenyTRequest.class.getSimpleName())) {
			return denyReq;
		} else if (type.equals(QueryItem.class.getSimpleName())) {
			return queryItem;
		} else if (type.equals(MyItems.class.getSimpleName())) {
			return new AuthenticatedMessageHandlerWrapper(myItem);
		} else if (type.equals(QueryUser.class.getSimpleName())) {
			return queryUser;
		} else if (type.equals(UserInfo.class.getSimpleName())) {
			return userInfo;
		} else if (type.equals(HeartBeat.class.getSimpleName())) {
			return heartbeathandle;
		} else if (type.equals(SearchItemQuery.class.getSimpleName())) {
			return searchItemQueryHandle;
		} else if (type.equals(SearchResult.class.getSimpleName())) {
			return new AuthenticatedMessageHandlerWrapper(searchResultHandle);
		} else if (type.equals(AbortCommit.class.getSimpleName())) {
			return new AuthenticatedMessageHandlerWrapper(abortTRequestHandle);
		} else if (type.equals(Commit.class.getSimpleName())) {
			return commitHandle;
		}else {
			return null;
		}

	}

}
